package com.ahuan.config;

import com.ahuan.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
@Component
public class InterceptorConfig implements WebMvcConfigurer {


    @Autowired
    TokenInterceptor tokenInterceptor;

    /**
     *  添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        ArrayList<String> excludePath = new ArrayList<>();
        excludePath.add("/api/login");//登录放行
        excludePath.add("/api/code");//验证码放行
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    /**
     *  配置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/images/**") // 定义访问路径
                .addResourceLocations("classpath:/static/"); // 指定静态资源所在目录
    }
}
